package com.teddy.customWidgets;
import com.teddy.util.Constants;

import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class SaveDialog extends Dialog {
	private String message;
	private String outputUri;
	private String outputFormat;
	  
	public SaveDialog(Shell parent, String uri) {
		 this(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		 outputUri = uri;
	}

	public SaveDialog(Shell parent, int style) {
		// Let users override the default styles
		super(parent, style);
		setText("Export file");
	}

	public String getMessage() {
		return message;
	}

 	public void setMessage(String message) {
 		this.message = message;
 	}

 	public String getOutputUri() {
 		return outputUri;
 	}

 	public void setOutputUri(String output) {
 		this.outputUri = output;
 	}

 	public String open() {
 		// Create the dialog window
 		Shell shell = new Shell(getParent(), getStyle());
 		shell.setText(getText());
 		createContents(shell);
	    shell.pack();
	    shell.open();
	    Display display = getParent().getDisplay();
	    while (!shell.isDisposed()) {
	      if (!display.readAndDispatch()) {
	        display.sleep();
	      }
	    }
	    // Return the entered value, or null
	    return outputUri;
 	}

 	private void createContents(final Shell shell) {
	    shell.setLayout(new GridLayout(2, false));
	
	    GridData data = new GridData();
	
	    // Display the input box
	    final Text text = new Text(shell, SWT.BORDER);
	    text.setSize(230, 40);
	    text.setText(outputUri);
	    data = new GridData(GridData.FILL_VERTICAL);
	    data.verticalSpan = 2;
	    text.setLayoutData(data);
	    
	    Button browse = new Button(shell, SWT.PUSH);
	    
	    browse.setText("...");
	    browse.setToolTipText("Browse");
	    data = new GridData(GridData.FILL_HORIZONTAL);
	    data.verticalSpan = 2;
	    browse.setLayoutData(data);
	    
	    browse.addSelectionListener(new SelectionListener() {
			
	    	public void widgetSelected(SelectionEvent event) {
	            FileDialog fd = new FileDialog(shell, SWT.SAVE);
	            fd.setText("Open");
	            fd.setFilterPath("C:/");
	            String[] filterExt = {Constants.THESAUR_FILTER};
	            fd.setFilterExtensions(filterExt);
	            outputUri = fd.open();
	            text.setText(outputUri);
	        }
	
	    	@Override
	    	public void widgetDefaultSelected(SelectionEvent e) {
	    	}
		});
	    
	    Label format = new Label(shell, SWT.NONE);
	    format.setText("RDF format:");
	    data = new GridData( SWT.RIGHT, SWT.FILL, true, true );
	    data.verticalSpan = 2;
	    format.setLayoutData(data);
	    
	    final Combo combo = new Combo(shell, SWT.READ_ONLY);
	    combo.add(Constants.RDF_XML_FORMAT);
	    combo.add(Constants.RDF_ABBREV_FORMAT);
	    combo.add(Constants.TURTLE_FORMAT);
	    combo.select(0);
	    
	    combo.addListener(SWT.DefaultSelection, new Listener() {
	        public void handleEvent(Event e) {
	           outputFormat = ((Combo)e.widget).getText();
	          }
	    });
	    
	    data = new GridData(GridData.FILL_HORIZONTAL);
	    data.verticalSpan = 2;
	    combo.setLayoutData(data);
	    //will set input
	    // to the entered value
	    Button ok = new Button(shell, SWT.PUSH);
	    ok.setText("OK");
	    data = new GridData( SWT.RIGHT, SWT.FILL, true, true );
	    data.minimumWidth = 80;
	    ok.setLayoutData(data);
	    ok.addSelectionListener(new SelectionAdapter() {
	    	public void widgetSelected(SelectionEvent event) {
		        outputUri = text.getText();
		        outputFormat = combo.getText();
		        shell.close();
	    	}
	    });
	
	    // Create the cancel button and add a handler
	    // so that pressing it will set input to null
	    Button cancel = new Button(shell, SWT.PUSH);
	    cancel.setText("Cancel");
	    data = new GridData(GridData.FILL_HORIZONTAL);
	    cancel.setLayoutData(data);
	    cancel.addSelectionListener(new SelectionAdapter() {
	    	public void widgetSelected(SelectionEvent event) {
		        outputUri = null;
		        shell.close();
	    	}
	    });
	
	    // Set the OK button as the default, so
	    // user can type input and press Enter
	    // to dismiss
	    shell.setDefaultButton(ok);
 	}

	public void setOutputFormat(String outputFormat) {
		this.outputFormat = outputFormat;
	}

	public String getOutputFormat() {
		return outputFormat;
}
}